/**
 * *****************************************************************************
 * Code by: fallinlove
 *
 * Class: Encode
 *
 * Chuc nang: ma hoa du lieu.
 *
 * Ngay tao: 05/03/2012
 *
 * Ngay sua doi: 05/03/2012
 *
 * Nguoi sua doi: fallinlove
 *
 ****************************************************************************
 */
package Controllers;

public class Encode {

    public static String dao_chuoi(String str) {
        String f2 = null;
        String f3 = null;
        char[] ch = str.toCharArray();//cho chuỗi vào mảng kí tự
        for (int i = str.length() - 1; i >= 0; i--) {
            f3 = String.copyValueOf(ch, i, 1);
            if (i == str.length() - 1) {
                f2 = f3;
            } else {
                f2 = f2.concat(f3);
            }
        }
        return f2;
    }

    public static String tach_chuoi(String str) {
        String f2 = "";
        String f3 = "";
        char[] ch = str.toCharArray();
        int i;
        for (i = 0; i <= str.length() - 1; i += 2) {
            f3 = String.copyValueOf(ch, i, 1);
            if (i == 0) {
                f2 = f3;
            } else {
                f2 = f2.concat(f3);
            }
        }
        f2 = f2.concat("*");
        for (i = 1; i <= str.length() - 1; i += 2) {
            f3 = String.copyValueOf(ch, i, 1);
            f2 = f2.concat(f3);
        }
        return f2;
    }

    public static String gop_chuoi(String str) {
        int n = str.indexOf("*");
        String c = str.substring(0, n);
        String l = str.substring(n + 1, str.length());
        int i;
        String f2 = null;
        String u = null;
        char[] ch1 = c.toCharArray();
        char[] ch2 = l.toCharArray();
        int max = c.length() > l.length() ? c.length() : l.length();
        for (i = 0; i <= max - 1; i++) {
            if (i <= c.length() - 1) {
                u = String.copyValueOf(ch1, i, 1);
                if (i == 0) {
                    f2 = u;
                } else {
                    f2 = f2.concat(u);
                }
            }
            if (i <= l.length() - 1) {
                u = String.copyValueOf(ch2, i, 1);
                if (i == 0) {
                    f2 = u;
                } else {
                    f2 = f2.concat(u);
                }
            }

        }
        return f2;
    }

    public static String encode_url_v3(String str, String key) {
        int i;
        String f2 = "";
        char c;
        int m;
        char[] ch = str.toCharArray();
        for (i = 0; i <= str.length() - 1; i++) {
            c = str.charAt(i);
            m = (int) c;
            if (i == 0) {
                f2 = Integer.toString(m + 42) + ":";
            } else {
                f2 = f2.concat(Integer.toString(m + 42) + ":");
            }
        }
        f2 = dao_chuoi(f2);
        f2 = tach_chuoi(f2);
        return key + "***" + f2;
    }

    public static String decode_url_v3(String str) {
        int n = str.indexOf("*");
        String l = str.substring(n + 3, str.length());
        String f = "";
        String f2 = "";
        f = gop_chuoi(l);
        f = dao_chuoi(f);
        int k, i = 0, m = 0, j;
        String[] d = f.split(":");
        while (i <= d.length - 1) {
            /*
             * k = f.indexOf(":"); l = f.substring(0,k); f =
             * f.substring(k+1,f.length());
             */
            m = Integer.parseInt(d[i]) - 42;
            char s = (char) m;
            if (i == 0) {
                f2 = String.valueOf(s);
            } else {
                f2 = f2.concat(String.valueOf(s));
            }
            i++;
        }
        return f2;
    }
}
